package main

import "fmt"

//type ListNode struct {
//	Val  int
//	Next *ListNode
//}

func main() {
	val := reverseKGroup(&ListNode{2, &ListNode{4, &ListNode{7, &ListNode{8, &ListNode{9, &ListNode{10, &ListNode{11, nil}}}}}}}, 2)
	fmt.Println("val:", printListNode(val))
}

func reverseKGroup(head *ListNode, k int) *ListNode {
	end := head
	for i := 0; i < k; i++ {
		if end == nil {
			return head
		}
		end = end.Next
	}
	newHead := reverse(head, end)
	head.Next = reverseKGroup(end, k)
	return newHead
}
func reverse(start, end *ListNode) *ListNode {
	var pre *ListNode
	curr := start
	for curr != end {
		next := curr.Next
		curr.Next = pre
		pre = curr
		curr = next
	}
	return pre
}

//func printListNode(head *ListNode) []int {
//	arr := make([]int, 0)
//	for head != nil {
//		arr = append(arr, head.Val)
//		head = head.Next
//	}
//	return arr
//}
